# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from dateutil.relativedelta import relativedelta

from odoo import api, fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.exceptions import UserError


class StockRule(models.Model):
    _inherit = 'stock.rule'
    #继承方法，在自动生成purchase.order时将采购订单日期设置=schedule_date而不考虑产品货期往前推，这样获得的采购价格的依据是销售订单日期
    def _get_purchase_order_date(self, product_id, product_qty, product_uom, values, partner, schedule_date):
        """Return the datetime value to use as Order Date (``date_order``) for the
           Purchase Order created to satisfy the given procurement. """
        seller = product_id.with_context(force_company=values['company_id'].id)._select_seller(
            partner_id=partner,
            quantity=product_qty,
            date=schedule_date and schedule_date.date(),
            uom_id=product_uom)
        # return schedule_date - relativedelta(days=int(seller.delay))
        return schedule_date #dbh 